package com.zjpavt.client.extenddevice.networkdevice;

import com.zjpavt.client.udp.UdpSender;
import lombok.extern.slf4j.Slf4j;

import java.text.DecimalFormat;

/**
 * @author zyc
 * 铜川音响
 */
@Slf4j
public class TongChuanAudio {
    public static final String DESTINATION_IP = "192.168.28.255";
    public static final int DESTINATION_PORT = 9036;
    public static final DecimalFormat DECIMAL_FORMAT = new DecimalFormat("00");
    public static int SCENE_ID = 0;
    public static void main(String[] args) {
        stopAt1();
    }
    /**
     * 发送suite场景。
     * 从文件中获取suite设备的IP地址 如果获取不到则使用默认的IP=192.168.1.220
     * 可以编辑suite_device_ip,指定UDP地址 第四位地址可以设置到255发UDP广播。
     * @param sceneId
     * @return
     */

    public static boolean sendSuiteScene(int sceneId, String ffff593143100000000110011400000000) {
        long startTime = System.currentTimeMillis();
        log.info("TongChuanAudio SCENE_ID =" + sceneId);
        String sSceneId = DECIMAL_FORMAT.format(sceneId);
        String audioCommand = ffff593143100000000110011400000000 + sSceneId + "0000";
        byte[] bytes = new byte[audioCommand.length() / 2];
        for (int i = 0; i < audioCommand.length() / 2; i++) {
            bytes[i] = (byte) (Character.digit(audioCommand.charAt(2 * i), 16) * 16
                               + Character.digit(audioCommand.charAt(2 * i + 1), 16));
        }
        UdpSender.sendUdpPacket(DESTINATION_IP, DESTINATION_PORT, bytes);
//        log.info("DESTINATION_IP = " + DESTINATION_IP + " SCENE_ID= " + sceneId+ " msg=" + audioCommand + " use time=" +(System.currentTimeMillis() -startTime));
        return true;
    }

    public static void stopSuiteScene(int sceneId) {
        sendSuiteScene(sceneId, "FFFF593143100000000110011700000000");
        sendSuiteScene(sceneId, "FFFF593143100000000110021700000000");
    }

    public static void pasueSuiteScene(int sceneId) {
        sendSuiteScene(sceneId, "FFFF593143100000000110011600000000");
        sendSuiteScene(sceneId, "FFFF593143100000000110021600000000");
    }

    public static void changeSuiteScene(int sceneId) {
        if (sceneId == SCENE_ID) {
            return;
        }
        sendSuiteScene(sceneId, "FFFF593143100000000110011400000000");
        sendSuiteScene(sceneId, "FFFF593143100000000110021400000000");
    }

    public static void stopAt1() {
        changeSuiteScene(1);
        stopSuiteScene(1);
        SCENE_ID = -1;

    }
}
